package com.rfsp.mcp.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rfsp.common.base.BaseService;
import com.rfsp.common.service.AreaService;
import com.rfsp.mcp.mapper.BargainHisMapper;
import com.rfsp.mcp.mapper.BargainMapper;
import com.rfsp.mcp.pojo.dto.BargainDTO;
import com.rfsp.mcp.pojo.po.BargainHisPO;
import com.rfsp.mcp.pojo.po.BargainPO;
import com.rfsp.mcp.pojo.sdto.BargainSDTO;
import com.rfsp.common.pojo.sdto.PageSDTO;
import com.rfsp.mcp.pojo.vo.BargainVO;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BargainService extends BaseService<BargainPO> {

    @Autowired
    BargainMapper mapper;

    @Autowired
    BargainHisMapper hisMapper;

    @Autowired
	AreaService areaCodeService;

    @Autowired
    SystemCodeService systemCodeService;

    public PageInfo<BargainVO> list(BargainSDTO sdto, PageSDTO pageSdto){
        if(pageSdto!=null){
            PageHelper.startPage(pageSdto.getPageNum(), pageSdto.getPageSize());
        }
        List<BargainVO> voList=mapper.list(sdto);
        PageInfo<BargainVO> page = new PageInfo(voList);
        return page;
    }

    public List<BargainVO> list(BargainSDTO sdto){
        List<BargainVO> voList=mapper.list(sdto);
        return voList;
    }

    public BargainVO getById(Long id){
        if(id==null){
            return null;
        }
        BargainVO vo=new BargainVO();
        BargainPO e=mapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(e,vo);
        return vo;
    }

    public String getLoadCode(String financeNo,Integer count){
        String loadCode=financeNo+count;
        if(count<10){
            loadCode=financeNo+"0"+count;
        }
        BargainSDTO sdto=new BargainSDTO();
        sdto.setLoanCode(loadCode);
        List<BargainVO> voList=this.list(sdto);
        if(voList.size()>0){
            return getLoadCode(financeNo,count+1);
        }
        return loadCode;
    };
    public Integer create(BargainDTO dto){
        dto.setAid(this.getAID());
        dto.setLoanCode(systemCodeService.getBussCode("bargain"));

        dto.setCreateDate(new Date());
        dto.setValidity("1");

        dto.setOptTime(new Date());
        dto.setOptType("1");


        BargainPO po=new BargainPO();
        BeanUtils.copyProperties(dto,po);
        Integer count=this.save(po);
        dto.setId(po.getId());

        //保存历史表
        BargainHisPO hisPO = new BargainHisPO();
        BeanUtils.copyProperties(po, hisPO);
        hisPO.setId(null);
        hisPO.setFkid(po.getId());
        hisMapper.insertSelective(hisPO);

        return count;
    }
    public void combo(BargainVO vo){
        vo.setContractCurrencyName("人民币元");
        
        //地区
        if(StringUtils.isNotBlank(vo.getAreaCode())) {
        	vo.setAreaName(areaCodeService.getFullAddress(vo.getAreaCode()));
        }
    }
    public void combo(List<BargainVO> voList){
        voList.stream().forEach(this::combo);
        voList.forEach(this::combo);
    }
}
